$(function(){
	var detailId=queryString('updateId');
	var flowId=queryString('flowid');
	requestDetail(detailId);
	getInformationFlow(detailId);
	var information={};
	//文件上传按钮
	$("#fileUp").click(function(){
		
		$("#loading").hide();
		$("#loadOk").hide();
	})
	$("#fileUp").on("change","#upfile-haoping",function(){
		fileup();
	})
	//确认修改按钮
	$("#requestUpdate").click(function(){
		getInformation();

	})
	//修改模态框确认按钮
	$("#update").click(function(){
		requestUpdate();//调用修改数据方法
		$("#updateModal").modal('hide');
	})
	$(document).on('click','.glyphicon-remove',function(){
		$(this).parent().remove();
	});


function getInformationFlow(id){
		var url="getInformationFlowByInfoId.do";
		var infoId={infoId:id};
		var object={};
		YuXi.util.get(url,infoId,function(data){
			if(data.status){
				object=data.object;
				var flow;
				
				for (var i = 0; i < object.length; i++) {
					if(object[i].flowId==3){
						flow="初审"
					}
					if(object[i].flowId==2){
						flow="初审"
					}
					if(object[i].flowId==4){
						flow="复核"
					}
					if(object[i].flowId==5){
						flow="审核"
					}
					var time = YuXi.getTime.get(object[i].createTime);
					
					if(object[i].remark==undefined){
						object[i].remark="";
					}
					var html = "<tr ><td style='border:none;'>" + object[i].userName + "</td>+" +
					
					"<td style='border:none;'>" +time  + "</td>" +
					"<td style='border:none;'>" + object[i].remark + "</td>" +
					
					"<td style='border:none;'>" +flow  + "</td>" +
 					"</tr>";
					$("#tbody").append(html);
				}
			}
		},function(){
			
		});
	}
//正则取链接传来的数据
function queryString(key)
{
    var regex_str = "^.+\\?.*?\\b"+ key +"=(.*?)(?:(?=&)|$|#)"
    var regex = new RegExp(regex_str,"i");
    var url = window.location.toString();
    if(regex.test(url)) return RegExp.$1;
    return undefined;
}


$(document).on("click", ".info-sub-a", function() {
		//var id = $(this).attr("data-id");
		var url="SubToFirstCheck.do";
		myTool.alert({		
			title:"提示",
			context:"是否确认提交信息?",
			isMask:true,
			callback:function(){
				YuXi.util.get(url, {infoId: detailId}, function(data) {
					if(data.status){
						myTool.alert({		
							title:"提示",
							context:"提交信息成功。",
							isMask:true,
							callback:function(){
								window.location.reload();
							
							}
						});
						window.location.href="submittedInfo.jsp";
					}
				},function(){
					
				});
				
			}
		});
		
	});



//根据id向后台请求数据详情
function requestDetail(id)
{	
	var url="getUserInformationById.do";//向后台请求数据id
	var object={}
	var sourceName;
	var personId;
	var personName;
	var dataid={informationId:id};
	YuXi.util.get(url,dataid,function(data){
		object=data.object;
		sourceName=object.informationSourceNames.split(",");
		personName=object.inputPerSonNames.split(",");
		personId=object.inputPersonID.split(",");
		setTimeout(function(){
			$("input[name='source']").each(function(){
				for(var i=0;i<sourceName.length;i++){
					if($(this).attr("data-na")==sourceName[i]){
						$(this).attr("checked","checked");
						break;
					}
				}
				
			});
		},30);
		
		addSelect(".sele",object.safetyPortNames);
		addSelect(".pes",object.informationClassificationNames);
		addSelect(".sort",object.informationSortNames);
		for(var i=0;i<personName.length;i++){
			if(personName[i]!=""){
				var html="<span class='parsbox'><span  class='glyphicon glyphicon-user' aria-hidden='true' ></span><span class='select-data-user' data-id="+personId[i]+">"+personName[i]+"</span>&nbsp;&nbsp;<span   class='glyphicon glyphicon-remove' aria-hidden='true'></span></span>"
		        $(".label1").append(html);
			}
			
		}
		$(".briefing").text(object.informationProfile);//简介
		$(".basic").text(object.standard);//标准
		$(".step").text(object.mesure);//措施
		$(".cont").text(object.informationConternt);//内容
		
		
	},function(){
		

	})

}

	//向后台提交附件，返回地址
	 function fileup (){
			$("#loading").show();
			 $("#loadOk").hide();
	    $.ajaxFileUpload({
	        type:"post",
	        secureuri:false,
	        dataType:"json",      
	        url:"/yuxi/userFileUp.do",
	        fileElementId:"upfile-haoping",
	        success:function(data, status){
	        	
	        	information.appendix=JSON.stringify(data.addressList);
	        	$("#loading").hide();
	        	$("#loadOk").show();
	        	$("#fileValue").remove();
	        	var html='<input id="upfile-haoping" style="position:absolute; right:0; top:0; font-size:100px; opacity:0; filter:alpha(opacity=0);" type="file" value="浏览" />';
	        	$("#fileUp").append(html);
	        },
	        error:function(data){
	        	
	          
	        } 
	        
	      });
	    }



function addSelect(className,value){
	setTimeout(function(){
		$(className+" option").each(function(){
			if($(this).html()==value){
				$(this).attr("selected","selected");
			}
		});
	},60);
	
}

function getInformation(){
	
	var check=[];
    var sourceName=[];
    var personId=[];
    var personName=[];
    $("input[name='source']:checked").each(function () {
    	check.push($(this).val());
        sourceName.push($(this).attr("data-na"));
            
     });
    $(".select-data-user").each(function(){
    	personId.push($(this).attr("data-id"));
		personName.push($(this).html());
		
		
	})
	information.id=detailId;
	portId=$(".sele option:selected").val()+",";
    information.informationSourceID=check.join(",");//渠道
    information.informationSourceNames=sourceName.join(",");
    information.safetyPortId=$(".sele option:selected").val(); //分口
    information.safetyPortNames=$(".sele option:selected").html(); 
    information.informationClassificationId=parseInt($(".pes option:selected").val());//分级
    information.informationClassificationNames=($(".pes option:selected").html());
    information.informationSortId=($(".sort option:selected").val());//分类
    information.informationSortNames=($(".sort option:selected").html());
    information.informationProfile=$(".briefing").text();//简介
    information.mesure=$(".step").text();//措施
    information.standard=$(".basic").text();//标准
    information.informationConternt=$(".cont").text();//内容
    information.inputPersonID=personId.join(",");//参与人
    information.inputPerSonNames=personName.join(",");
   if(!isNull(information.informationSourceID)){
    	myTool.alert({		
			title:"提示",
			context:"修改失败,渠道不能为空。",
			isMask:true,
			callback:function(){
				}
			});

    	YuXi.scrollbar.scroll("#resour");
    	return false;
    }
    if(!isNull(information.safetyPortId)){
    	$("#badmodal").modal('show');
    	return false;
    }
    if(!isNull(information.informationClassificationId)){
    	$("#badmodal").modal('show');
    	return false;
    }
    if(!isNull(information.informationSortId)){
    	$("#badmodal").modal('show');
    	return false;
    }
    if(!isNull(information.informationProfile)){
    	myTool.alert({		
			title:"提示",
			context:"修改失败,简介不能为空。",
			isMask:true,
			callback:function(){
				}
			});

    	YuXi.scrollbar.scroll("#brie");
    	return false;
    }
    if(!isNull(information.informationConternt)){
    	myTool.alert({		
			title:"提示",
			context:"修改失败,内容不能为空。",
			isMask:true,
			callback:function(){
				}
			});

    	YuXi.scrollbar.scroll("#content");
    	return false;
    }
    myTool.alert({		
		title:"提示",
		context:"是否确认提交修改后的信息?",
		isMask:true,
		callback:function(){
			
			requestUpdate(information);
		}
	});
    
}

/*判断输入合法性*/
function isNull(str){
	if(str==null||str==""){
		return false;
	}else{
		return true;
	}
}


function modal(msg,cont){
	myTool.alert({		
		title:msg,
		context:cont,
		isMask:true,
		callback:function(){}
	});
}

//修改分口，分类，分级，标准，措施
function requestUpdate(information)
{	var  flowName="";
	if(flowId==1){
		flowName="InfoManage";
	}
	if(flowId==2){
		flowName="FirstCheck";
	}
	if(flowId==3){
		flowName="ReCheck";
	}
	if(flowId==4){
		flowName="Check";
	}
	if(flowId==5){
		flowName="InfoResearch";
	}
	if(flowId==6){
		flowName="TaskDispatch";
	}
	if(flowId==7){
		flowName="CurTaskDispatch";
	}
	var url=flowName+"/userUpdataInformation.do";//修改的地址;
	YuXi.util.get(url,information,function(data){
		if(data.status)
		{
			myTool.alert({		
				title:"提示",
				context:"信息修改成功！",
				isMask:true,
				callback:function(){
					window.location.href="submittedInfo.jsp?menuId=2";
				}
			});
		}
		else
		{
			myTool.alert({		
				title:"提示",
				context:"信息修改失败！",
				isMask:true,
				callback:function(){}
			});
		}
	},function(){
		myTool.alert({		
			title:"提示",
			context:"信息修改失败！",
			isMask:true,
			callback:function(){}
		});
	})
}

//删除
$(document).on("click", ".delete", function() {	
			//deleteid = $(this).attr("id");
			myTool.alert({
				title:"删除确认",
				context:"你确定要删除此条信息吗?",
				callback:function(){
					requestDele(detailId);
				},
				isMask:true
			});
		})

//删除确认
	function requestDele(id) {
		var dataid = {
			id: id
		};
		var url = "userDelInformationById.do" //删除信息地址;
		YuXi.util.get(url, dataid, function(data) {
			//$("#" + id).parents("tr").remove();
			$("#successmodel").modal('show');
			window.location.href="submittedInfo.jsp";

		}, function() {
			$("#badmodal").modal('show');
		})
	}

})